<template>
  <div>
    <el-form ref="Form" :model="Form" :rules="Rules" label-width="140px" class="business-form" auto-complete="on">
       <el-form-item prop="code" label="激活码">
         <el-input style="width:263px" class="default-input" v-model="Form.code" placeholder="请输入激活码" />
         <el-button type="primary" @click="getCode()">
           获取激活码
         </el-button>
       </el-form-item>
       <el-form-item prop="isActivation" label="是否激活">
         <el-radio v-model="Form.isActivation" :label=false>否</el-radio>
         <el-radio v-model="Form.isActivation" :label=true>是</el-radio>
       </el-form-item>
       <el-form-item label="是否失效">
         <el-radio v-model="Form.isExpire" :label=false>否</el-radio>
         <el-radio v-model="Form.isExpire" :label=true>是</el-radio>
       </el-form-item>

       <el-form-item>
         <el-button class="btn-submit" type="primary" v-if="id" @click.native.prevent="handleAdd">
           编辑
         </el-button>
         <el-button class="btn-submit" type="primary" v-else @click.native.prevent="handleAdd">
           新增
         </el-button>
       </el-form-item>
     </el-form>
  </div>
</template>

<script>
import { add, update, getCode } from '@/api/business/ckycode'
export default {
  data() {
    return {
      Form: {
        code: null,
        isActivation: false,
        isExpire: false
      },
      Rules: {
        code: [{
          required: true, trigger: 'blur', message: '请输入激活码'
        }],
        isActivation: [{
          required: true, trigger: 'blur', message: '是否已激活'
        }]
      },
      id: this.getUrlParams('id'),
    }
  },
  created() {
    if(this.id){
      this.getData();
    }
  },
  methods: {
    getCode() {
      getCode().then(res=>{
        this.Form.code = res
      })
    },
    getData() {
      this.Form = this.getlocalStorage('bckycode');
    },
    handleAdd() {
      this.$refs.Form.validate(valid => {
       if (valid) {

         if(this.id){
           this.Form.id = this.id
           update(this.Form).then(res=>{
             this.actionsAfter('编辑成功！', 'success', '-1');
           })
         }else{
           add(this.Form).then(res=>{
             this.actionsAfter('新增成功！', 'success', '-1');
           })
         }

       }
     })
    }
  }
}
</script>

<style lang="css">
</style>
